/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.hunk.struts.action;

import com.sun.hunk.hibernate.DAOFactory;
import com.sun.hunk.hibernate.domain.Comment;
import com.sun.hunk.hibernate.domain.User;
import com.sun.hunk.struts.HunkDispatchAction;
import com.sun.hunk.struts.form.CommentForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author sunil
 */
public class CommentAction extends HunkDispatchAction {

    public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (isLoggedInUser(request)) {
            CommentForm commentForm = (CommentForm) form;
            Comment comment = commentForm.getComment();
            User user = (User)request.getSession().getAttribute(USER);
            comment.setUser(user);
            DAOFactory.getCommnetDAO().saveOrUpdate(comment);
            setMessage(request, "Thanks for your comment !");
            return mapping.findForward("message");
        } else {
            setError(request, "You should login to leave comment !");
            return mapping.findForward("message");
        }
    }
}
